// Example 11.2
// rotary encoder demonstration
// http://tronixstuff.com/tutorials > Chapter 11
//
// Uses code from Sparkfun website, no author or licensing information
// from: http://bit.ly/a9OH2x
//
// Connect the middle pin of the three to ground, and also one of the pair on the other side
// The outside two pins of the three are connected to digital pins 7 and 5
// The other button pin is connected to digital pin 3

int dpInEncoderA=2;
int dpInEncoderB=3;
int dpInEncoderPress=4;

static void _ResetPins()
{
  // Rotary encoder input lines
  // Configure as input, turn on pullup resistors
  pinMode(dpInEncoderA, INPUT);
  digitalWrite(dpInEncoderA, HIGH);
  pinMode(dpInEncoderB, INPUT);
  digitalWrite(dpInEncoderB, HIGH);
  pinMode(dpInEncoderPress, INPUT);
  digitalWrite(dpInEncoderPress, HIGH);
}

void _lowlevel_ReadEncoder(int &rotate, int& press)
{
  rotate = (digitalRead(dpInEncoderB) * 2) + digitalRead(dpInEncoderA);
  press = digitalRead(dpInEncoderPress);
}

void ReadEncoder()
{
  int Position, Press;

  
  _ResetPins();
  Serial.println("Reading the encoder... press a key to abort.");
  _lowlevel_ReadEncoder(Position, Press);
  while (!Serial.available())
  {
    int Position2, Press2;
    do
    {
      _lowlevel_ReadEncoder(Position2, Press2);
    } while ((Position2 == Position) && (Press2 == Press));
    if (Position2 != Position)
    {
      // "Forward" is shown by the position going from (0 to 1) or (1 to 3)
      // or (3 to 2) or (2 to 0).  Anything else indicates that the user is
      // turning the device the other way.  Remember: this is Gray code, not
      // binary.
      int isFwd = ((Position == 0) && (Position2 == 1)) ||
                  ((Position == 1) && (Position2 == 3)) ||
                  ((Position == 3) && (Position2 == 2)) ||
                  ((Position == 2) && (Position2 == 0));
      Serial.println(isFwd ? "FWD " : "BWD ");
      Serial.println(isFwd);
    }
    if (Press2 != Press)
    {
      Serial.println(Press ? "Press " : "Release ");
    }
    Position = Position2;
    Press = Press2;
  }
}

void setup()
{
  // configure the pins
  _ResetPins();

  // init serial communication
  Serial.begin(115200); 
  Serial.println("Ready to begin");
}

void loop()
{
  ReadEncoder();
}
